package com.vf.admin.fiwork.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vf.admin.common.jiami.MD5Utils;

import static com.vf.admin.utils.RandomUtil.getOrderIdByUUId;

/**
 * 进行订单的第三方请求
 */
public class HttpOrderUtil {

    /**
     *  创建订单
     * @param url 地址
     * @param params 排序后的参数
     * @param sign 签名
     * @return
     */
    public static String doJsonPost(String url, String params, String sign){
        HttpURLConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        BufferedReader br = null;
        String result = null;
        try{
            URL url1 = new URL(url);
            // 通过远程url连接对象打开连接
            connection = (HttpURLConnection) url1.openConnection();
            // 设置连接请求方式
            connection.setRequestMethod("POST");
            // 设置连接主机服务器超时时间：15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取主机服务器返回数据超时时间：60000毫秒
            connection.setReadTimeout(60000);
            // 默认值为：false，当向远程服务器传送数据/写数据时，需要设置为true
            connection.setDoOutput(true);
            // 默认值为：true，当前向远程服务读取数据时，设置为true，该参数可有可无
            connection.setDoInput(true);
            // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 通过连接对象获取一个输出流
            os = connection.getOutputStream();
            // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的(form表单形式的参数实质也是key,value值的拼接，类似于get请求参数的拼接)
            os.write((params+"&sign="+sign).getBytes());
            // 通过连接对象获取一个输入流，向远程读取
            if (connection.getResponseCode() == 200) {

                is = connection.getInputStream();
                // 对输入流对象进行包装:charset根据工作项目组的要求来设置
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                StringBuffer sbf = new StringBuffer();
                String temp = null;
                // 循环遍历一行一行读取数据
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        }catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 断开与远程地址url的连接
            connection.disconnect();
        }
        return result;
    }

    /**
     * 测试接口信息
     * @param args
     */
    public static void main(String[] args) {
        String url ="https://api.serpayment.com/cashier/pay.ac";
        Map m = new HashMap();
        TreeMap<String,Object> params = new TreeMap<>();
        params.put("custId", "20121600002118");
        params.put("custOrderNo", getOrderIdByUUId());
        params.put("orgNo", "8201200544");
        params.put("tranType","0101");
        params.put("clearType","01");
        params.put("payAmt", "10000");
        params.put("backUrl","http://123.160.134.65:80/app/recharge/payment");
        params.put("frontUrl","http://123.160.134.65:80/app/recharge/payment");
        params.put("bankCode","ANDB");
        params.put("goodsName","zshi");
        params.put("orderName","zshicz");
        params.put("buyIp","127.0.0.1");
        params.put("userName","zhangsan");
        params.put("userEmail","18378177555@163.com");
        params.put("userPhone","12738795846");
        params.put("userCitizenId","");
        params.put("userDeviceId","12738795846");
        params.put("countryCode","IN");
        params.put("currency","INR");
        params.put("city","IN");
        params.put("street", "04");
        params.put("houseNumber", "02");
        params.put("version", "2.1");
        params.put("orderDesc","this is orderDesc");
        String s3 = ASCIIUtils.ASCIIEncryption(params);
        String key ="7D2FC633056ED63FEBA136EE777A81AC";
        String sign = MD5Utils.md5Generate(s3+"&key="+key);
        System.out.println("-----------------------------------------------------");
        System.out.println(url+"?"+s3+"&sign="+sign);
        System.out.println(sign);
        String s2 = doJsonPost(url, s3,sign);
        JSONObject jsonObject = JSON.parseObject(s2);
        System.out.println(jsonObject.getString("code"));
        System.out.println(jsonObject.getString("msg"));
        System.out.println(s2);
    }



}
